import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    tbasList: [], //Tab栏列表
    userList: [], //用户列表
    cacheView: [], //缓存列表
    frameType: "", //判断图形形状
    figureParameter: {}, //图形参数
    beforeId: "", //当前选中id
    imgSrc: "",
    sourcesInfo: {
      sourcesID: "", //数据来源
    },
    fieldInfo: {},
    localityimgurl: "",
    networkimgurl: "",
    labelInfo: {},
    labeltypeId: "",
    tags: [],
    startPrint: false,

  },
  getters: {},
  mutations: {
    //本地图片
    localityFun(state, parmas) {
      state.tags.map((item, index) => {
        if (item.id == state.beforeId) {
          item.data.localityImg = parmas.files;
          item.data.localityname = parmas.localityimgurl;
        }
      });
      state.localityimgurl = parmas.localityimgurl;
    },
    //网络图片
    networkFun(state, parmas) {
      state.tags.map((item, index) => {
        if (item.id == state.beforeId) {
          item.data.networkImg = parmas;
        }
      });
      state.networkimgurl = parmas.parmas;
    },
    AddTabs(state, parmas) {
      state.tbasList.push(parmas);
      window.sessionStorage.setItem("list", JSON.stringify(state.tbasList));
    },
    SynchronizeTabs(state, parmas) {
      state.tbasList = parmas;
      window.sessionStorage.setItem("list", JSON.stringify(state.tbasList));
    },
    UserList(state, parmas) {
      state.userList = parmas;
      window.sessionStorage.setItem("userList", JSON.stringify(state.userList));
    },
    ADD_CACHE_VIEW: (state, view) => {
      state.cacheView.push(view);
    },
    DELETE_CACHE_VIEW: (state, index) => {
      state.cacheView.splice(index, 1);
    },
    All_DELETE_CACHE_VIEW: (state, index) => {
      state.cacheView = [];
      state.tbasList = [];
    },
    //修改点击获取的图形类型
    changeframeType(state, data) {
      state.frameType = data;
    },
    //修改点击获取图形参数
    changeParameter(state, data) {
      state.figureParameter = data;
    },
    //上传的图片
    changeImg(state, data) {
      state.localityimgurl = data;
    },
    changebeforeId(state, data) {
      state.beforeId = data;
    },
    //数据源选择的信息
    changeSources(state, data) {
      //1为数据来源
      if (data.type == 1) {
        state.tags.map((item, index) => {
          if (item.id == state.beforeId) {
            item.data.sourcesID = data.value;
          }
        });
        state.sourcesInfo.sourcesID = data.value;
        //2为数据字段
      } else if (data.type == 2) {
        state.tags.map((item, index) => {
          if (item.id == state.beforeId) {
            item.data.dataFieldID = data.value.dataFieldID;
            item.data.dictionaryName = data.value.dictionaryName;
            item.data.sourceName = data.value.dataFieldName;
          }
        });
        state.fieldInfo = data.value;
        //3为数据关系字段
      }
    },
    changeLabel(state, data) {
      state.labelInfo = data;
    },
    //修改双向绑定的图片数据
    changeImgs(state, data) {
      state.localityimgurl = data.localityimgurl;
      state.networkimgurl = data.networkimgurl;
    },
    //修改点击的双向绑定下拉框数据
    changeencoded(state, data) {
      state.sourcesInfo.sourcesID = data.sourcesID;
      state.fieldInfo.dataFieldID = data.dataFieldID;
      state.fieldInfo.dictionaryName = data.dictionaryName;
    },
    //清空
    emptyFun(state, data) {
      (state.beforeId = null), (state.figureParameter = {});
      state.frameType = "";
      (state.sourcesInfo = {}),
        (state.fieldInfo = {}),
        (state.localityimgurl = ""),
        (state.networkimgurl = "");
      state.tags = [];
    },
    upPrintShow(state, value) {
      state.startPrint = value
    }
  },
  actions: {
    // 动态添加及删除缓存
    addCacheView({ commit, state }, view) {
      if (!state.cacheView.includes(view)) {
        commit("ADD_CACHE_VIEW", view);
      }
    },
    deleteCacheView({ commit, state }, view) {
      const index = state.cacheView.indexOf(view);
      if (index > -1) {
        commit("DELETE_CACHE_VIEW", index);
      }
    },

  },
  modules: {},
});
